package com.mydeershow.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;

import com.paomi.client.R;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.bean.SetBeanPay;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.SPUtil;
import com.paomi.client.widget.RxToast;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX;
import com.tencent.mm.opensdk.modelmsg.WXAppExtendObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by pc on 2018/12/5
 * Describe:微信支付的回调
 * com.paomi.client.wxapi
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    // IWXAPI 是第三方app和微信通信的openapi接口
    private IWXAPI api;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.empty_test);
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        // 将该app注册到微信
        api.registerApp(Constants.APP_ID);
        //注意：
        //第三方开发者如果使用透明界面来实现WXEntryActivity，需要判断handleIntent的返回值，如果返回值为false，则说明入参不合法未被SDK处理，应finish当前透明界面，避免外部通过传递非法参数的Intent导致停留在透明界面，引起用户的疑惑
        try {
            api.handleIntent(getIntent(), this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);
        api.handleIntent(intent, this);
    }

    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq baseReq) {
        switch (baseReq.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
//                goToGetMsg();
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                goToShowMsg((ShowMessageFromWX.Req) baseReq);
                break;
            default:
                break;
        }

    }

    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp baseResp) {
        if(baseResp.getType()==ConstantsAPI.COMMAND_PAY_BY_WX){
            Log.d("支付回调","onPayFinish,errCode="+baseResp.errCode);

        }
        SetBeanPay baseJSON=new SetBeanPay();
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                baseJSON.setMessage("支付成功");
                baseJSON.setCodeData(Constants.wxSuccess);
                RxToast.info(getResources().getString(R.string.pay_sure));
                break;
            case BaseResp.ErrCode.ERR_COMM:
                baseJSON.setMessage("支付失败");
                baseJSON.setCodeData(Constants.wxFailure);
                RxToast.info(getResources().getString(R.string.pay_failure));
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                baseJSON.setMessage("支付取消");
                baseJSON.setCodeData(Constants.wxCancel);
                RxToast.info(getResources().getString(R.string.pay_cancel_get));
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                break;
            case BaseResp.ErrCode.ERR_UNSUPPORT:
                break;
            default:
                break;
        }
        finish();
        new Handler().postDelayed(new Runnable() {
            public void run() {
                EventBus.getDefault().post(baseJSON);
            }
        }, 100);


    }
    private void goToShowMsg(ShowMessageFromWX.Req showReq) {
        WXMediaMessage wxMsg = showReq.message;
        WXAppExtendObject obj = (WXAppExtendObject) wxMsg.mediaObject;

        StringBuffer msg = new StringBuffer(); // 组织一个待显示的消息内容
        msg.append("description: ");
        msg.append(wxMsg.description);
        msg.append("\n");
        msg.append("extInfo: ");
        msg.append(obj.extInfo);
        msg.append("\n");
        msg.append("filePath: ");
        msg.append(obj.filePath);
        RxToast.info(msg.toString());
    }
}
